﻿using Gym.Application.Common.Interfaces;
using Gym.Application.Profiles.Common;
using Gym.Domain.AdminAggregate;
using Gym.Domain.Profiles;
using Microsoft.EntityFrameworkCore;

namespace Gym.Infrastructure.Persistence.Repositories
{
    public class AdminsRepository : IAdminsRepository
    {
        private readonly GymDbContext _dbContext;

        public AdminsRepository(GymDbContext dbContext)
        {
            _dbContext = dbContext;
        }

        public async Task AddAdminAsync(Admin admin)
        {
            await _dbContext.Admins.AddAsync(admin);
            await _dbContext.SaveChangesAsync();
        }

        public async Task<Admin?> GetByIdAsync(Guid adminId)
        {
            return await _dbContext.Admins.AsNoTracking().FirstOrDefaultAsync(a => a.Id == adminId);
        }

        public async Task<Profile?> GetProfileByUserIdAsync(Guid userId)
        {
            var admin = await _dbContext.Admins
                .AsNoTracking()
                .FirstOrDefaultAsync(a => a.UserId == userId);

            return admin is null ? null : new Profile(admin.Id, ProfileType.Admin);
        }

        public async Task UpdateAsync(Admin admin)
        {
            _dbContext.Update(admin);
            await _dbContext.SaveChangesAsync();
        }
    }
}
